package mumu.android.view;


import mumu.android.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

/**
 * 之前有介绍了dialog的弹出窗体。
 * 在某些情况下并不需要使用到dialog，popupwindow相对更为轻量级，且代码更为简洁方便，在不需要dialog的特性的时候可以用popupwindow替代dialog来实现弹出窗口。
 * @author Administrator
 *
 */
public class popupwindow1 extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(this.getClass().getName());
		setContentView(R.layout.popupwindow1);

		findViewById(R.id.btn_1).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showBottomMenu2();
			}
		});
		findViewById(R.id.btn_2).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showBottomMenu();
			}
		});
		findViewById(R.id.btn_c).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mPopupWindow != null)
					mPopupWindow.dismiss();
			}
		});
		
		findViewById(R.id.btn_3).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showBottomMenu3();
			}
		});
	}

	PopupWindow mPopupWindow;

	/**
	 * 点击其他区域隐藏控件
	 */
	public void showBottomMenu() {
		Context mContext = this;
		LayoutInflater mLayoutInfalter = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		View menuView = mLayoutInfalter.inflate(R.layout.mumu, null);
		mPopupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		// 设置如下四条信息，当点击其他区域使其隐藏，要在show之前配置
		mPopupWindow.setFocusable(true);
		mPopupWindow.setTouchable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
		// 设置SelectPicPopupWindow弹出窗体动画效果
		mPopupWindow.setAnimationStyle(R.style.PopupScaleAnimation);
		int[] location = new int[2];
		menuView.getLocationOnScreen(location);
		mPopupWindow.showAtLocation(findViewById(R.id.ly_c), Gravity.NO_GRAVITY, 0, 200);
	}

	/**
	 * 点击取消隐藏
	 */
	public void showBottomMenu2() {
		Context mContext = this;
		LayoutInflater mLayoutInfalter = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		View menuView = mLayoutInfalter.inflate(R.layout.mumu, null);
		mPopupWindow = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
		// 设置SelectPicPopupWindow弹出窗体动画效果
		mPopupWindow.setAnimationStyle(R.style.PopupScaleAnimation);
		int[] location = new int[2];
		menuView.getLocationOnScreen(location);
		mPopupWindow.showAtLocation(findViewById(R.id.ly_c), Gravity.RIGHT|Gravity.TOP, 200,150);
	}
	
	
	/**
	 * 点击取消隐藏
	 */
	public void showBottomMenu3() {
		Context mContext = this;
		LayoutInflater mLayoutInfalter = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		View menuView = mLayoutInfalter.inflate(R.layout.mumu, null);
		mPopupWindow = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.update();
		mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.RED));
		// 设置SelectPicPopupWindow弹出窗体动画效果
		//mPopupWindow.setAnimationStyle(R.style.PopupScaleAnimation);
		mPopupWindow.showAsDropDown(findViewById(R.id.ly_c),10,4);
	}
}
